package com.example.util;

import com.example.domain.entity.User;
import com.example.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author readpage
 * @date 2023-08-09 14:23
 */

@Component
public class AuthUtils {

    /**
     * 返回SpringSecurity的身份认证信息
     * @return org.springframework.security.core.Authentication
     */
//    public static User get() {
//        User user;
//        try {
//            String userKey = ServletUtils.getRequest().getHeader("User-Key");
//            if (userKey == null) {
//                return getAnonymous();
//            }
//            user = ConvertUtils.cloneDeep(userKey, User.class);
//        } catch (Exception e) {
//            user = getAnonymous();
//        }
//        return user;
//    }

    @Resource
    private UserService userService;

    private static UserService userUtil;

    @PostConstruct
    public void init() {
        userUtil = userService;
    }

    // 模拟获取token认证信息
    public static User get() {
        User user = userUtil.getById(1);
        return user;
    }


    public static User getAnonymous() {
        User user = new User();
        user.setUsername("anonymous");
        return user;
    }

}
